package utils

import (
	"encoding/hex"
	"errors"
	"fmt"
	"net"
	"runtime"
	"strings"

	"gitee.com/wulalade/hwpack/nmcli"
	"github.com/gogf/gf/os/gproc"
)

//获取设备本机网口的连接状态
func GetEthStatus(lan string) (bool, error) {
	var status bool
	var query string
	interfaces, err := net.Interfaces()
	if err != nil {
		fmt.Println("Error : ", err.Error())
		return status, err
	}
	//如果输入数据为空判断哪个平台
	if lan == "" {
		switch runtime.GOOS {
		case "linux":
			query = "eth0"
		case "windows":
			query = "以太网"
		}
	} else {
		query = lan
	}

	for _, inter := range interfaces {
		if inter.Name == query {
			flags := inter.Flags.String()
			if strings.Contains(flags, "up") {
				status = true
			}
			break
		}
	}
	return status, err
}

//获取设备本机的ip地址
func GetEthIp(lan string) string {
	var ip string
	var query string
	interfaces, err := net.Interfaces()
	if err != nil {
		fmt.Println("Error : ", err.Error())
		return ip
	}

	//如果输入数据为空判断哪个平台
	if lan == "" {
		switch runtime.GOOS {
		case "linux":
			query = "eth0"
		case "windows":
			query = "以太网"
		}
	} else {
		query = lan
	}

	for _, inter := range interfaces {
		if inter.Name == query {
			addrs, _ := inter.Addrs()
			for _, address := range addrs {
				if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
					if ipnet.IP.To4() != nil {
						ip = ipnet.IP.String()
						break
					}
				}
			}
		}
	}
	return ip
}

//获取设备本机的mac地址
func GetEthMac(lan string) (string, error) {
	var srcMac string
	var query string

	interfaces, err := net.Interfaces()
	if err != nil {
		fmt.Println("Error : ", err.Error())
		return srcMac, err
	}
	//如果输入数据为空判断哪个平台
	if lan == "" {
		switch runtime.GOOS {
		case "linux":
			query = "eth0"
		case "windows":
			query = "以太网"
		}
	} else {
		query = lan
	}

	for _, inter := range interfaces {
		if inter.Name == query {
			srcMac = hex.EncodeToString(inter.HardwareAddr) //获取本机MAC地址
			//fmt.Println("MAC = ", hex.EncodeToString(inter.HardwareAddr))
			break
		}
	}
	return srcMac, err
}

//获取设备本机的ip地址[]byte格式
func GetEthIpByte(lan string) []byte {
	ip := make([]byte, 0)
	var query string
	interfaces, err := net.Interfaces()
	if err != nil {
		fmt.Println("Error : ", err.Error())
		return ip
	}

	//如果输入数据为空判断哪个平台
	if lan == "" {
		switch runtime.GOOS {
		case "linux":
			query = "eth0"
		case "windows":
			query = "以太网"
		}
	} else {
		query = lan
	}

	for _, inter := range interfaces {
		if inter.Name == query {
			addrs, _ := inter.Addrs()
			for _, address := range addrs {
				if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
					if ipnet.IP.To4() != nil {
						ip = ipnet.IP.To4()
						break
					}
				}
			}
		}
	}
	return ip
}

//获取设备本机的mac地址 []byte格式
func GetEthMacByte(lan string) []byte {
	srcMac := make([]byte, 0)
	var query string

	interfaces, err := net.Interfaces()
	if err != nil {
		return srcMac
	}
	//如果输入数据为空判断哪个平台
	if lan == "" {
		switch runtime.GOOS {
		case "linux":
			query = "eth0"
		case "windows":
			query = "以太网"
		}
	} else {
		query = lan
	}

	for _, inter := range interfaces {
		if inter.Name == query {
			srcMac = append(srcMac, inter.HardwareAddr...)
			//fmt.Println("MAC = ", hex.EncodeToString(inter.HardwareAddr))
			break
		}
	}
	return srcMac
}

//设置dns
func DnsSet(dns1, dns2 string) {
	if dns1 != "" && dns2 != "" {
		gproc.ShellExec(`echo -e  "nameserver ` + dns1 + "\nnameserver " + dns2 + `" > /etc/resolv.conf`)
	} else if dns1 != "" {
		gproc.ShellExec(`echo -e  "nameserver ` + dns1 + `" > /etc/resolv.conf`)
	} else if dns2 != "" {
		gproc.ShellExec(`echo -e  "nameserver ` + dns2 + `" > /etc/resolv.conf`)
	} else {
		gproc.ShellExec(`echo -e  "nameserver 10.10.10.1\nnameserver 218.85.152.99" > /etc/resolv.conf`)
	}
}

//修改/etc/network/interfaces文件
func ChangeNetworInterfaces(newIP, newGate, newMak string) {
	if newIP == "" ||
		newGate == "" ||
		newMak == "" {
		return
	}
	//srcIp, _ := GetEthIp()
	//ipChange.IpChange(newIP, newMak, newGate)
	//run1 := fmt.Sprintf("sed -i '/%s/ {s/%s/%s/g;}/' /etc/network/interfaces", "address", "", newIP)
	//run2 := fmt.Sprintf("sed -i '/%s/ {s/%s/%s/g;}/' /etc/network/interfaces", "netmask", "", newMak)
	//run3 := fmt.Sprintf("sed -i '/%s/ {s/%s/%s/g;}/' /etc/network/interfaces", "gateway", "", newGate)
	platform := GetPlatform()
	switch platform {
	case Linux_nano:
		run1 := fmt.Sprintf("sed -i '11s/%s/%s/' /etc/network/interfaces", ".*", "address "+newIP)
		run2 := fmt.Sprintf("sed -i '12s/%s/%s/' /etc/network/interfaces", ".*", "netmask "+newMak)
		run3 := fmt.Sprintf("sed -i '13s/%s/%s/' /etc/network/interfaces", ".*", "gateway "+newGate)

		gproc.ShellExec(run1)
		gproc.ShellExec(run2)
		gproc.ShellExec(run3)
	case Linux_nano46:
		//通过nmcli修改网卡信息
		eths, err := nmcli.ListConnections(false)
		if err == nil {
			for _, eth := range eths {
				if eth.Device == "eth0" {
					set := nmcli.IpDefaults{
						DNS:         "114.114.114.114",
						Ipv4Address: fmt.Sprintf("%s/%d", newIP, nmcli.StringIpToInt(newMak)), //默认为255.255.255.0
						Ipv4Gateway: newGate,
					}
					//fmt.Println("set : ", set)
					nmcli.ModifyIp(eth.Name, set)
					nmcli.UpDownConnect(eth.Name, nmcli.UP)
					break
				}
			}
		}
	}

}

//获取设备本机的mac地址
func GetInterfaces(lan string) (net.Interface, error) {
	var query string
	interEth := net.Interface{}
	interfaces, err := net.Interfaces()
	if err != nil {
		//fmt.Println("Error : ", err.Error())
		return interEth, errors.New("not found")
	}
	//如果输入数据为空判断哪个平台
	if lan == "" {
		switch runtime.GOOS {
		case "linux":
			query = "eth0"
		case "windows":
			query = "以太网"
		}
	} else {
		query = lan
	}

	for _, inter := range interfaces {
		//fmt.Println(inter.Name)
		if inter.Name == query {
			interEth = inter
			return interEth, nil
		}
	}
	return interEth, errors.New("not found")
}
